package com.example.myapplication.navigation

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navigation
import com.example.myapplication.control.AddCountScreen
import com.example.myapplication.control.ControlScreen
import com.example.myapplication.control.PlanAmountScreen
import com.example.myapplication.mine.AboutVersionPage
import com.example.myapplication.mine.AccountAndSecurityPage
import com.example.myapplication.mine.AppSettingPage
import com.example.myapplication.mine.LoginMainScreen
import com.example.myapplication.mine.MineScreen
import com.example.myapplication.navigation.AppRoute.PLAN_AMOUNT
import com.example.myapplication.shop.ShopScreen
import com.example.myapplication.smart.SmartScreen

@Composable
fun AppHost(navController : NavHostController){
    NavHost(navController, startDestination = AppRoute.CONTROL){
        //控制页tab
        navigation(startDestination = AppRoute.CONTROL_MAIN, route = AppRoute.CONTROL) {
            composable(route = AppRoute.CONTROL_MAIN) {
                ControlScreen()
            }

            composable(route = AppRoute.ADD_ACCOUNT){
                AddCountScreen()
            }

            composable(route = AppRoute.PLAN_AMOUNT){
                PlanAmountScreen()
            }
        }

        navigation(startDestination = AppRoute.SMART_MAIN, route = AppRoute.SMART) {
            composable(route = AppRoute.SMART_MAIN) {
                SmartScreen()
            }
        }

        navigation(startDestination = AppRoute.SHOP_MAIN, route = AppRoute.SHOP) {
            composable(route = AppRoute.SHOP_MAIN) {
                ShopScreen()
            }
        }

        navigation(startDestination = AppRoute.MINE_MAIN, route = AppRoute.MINE) {
            composable(route = AppRoute.MINE_MAIN) {
                MineScreen()
            }

            composable(route = AppRoute.ACCOUNT_AND_SECURITY){
                AccountAndSecurityPage()
            }

            composable(route = AppRoute.APP_SETTING) {
                AppSettingPage()
            }

            composable(route = AppRoute.APP_ABOUTVERSION) {
                AboutVersionPage()
            }

            composable(route = AppRoute.loginMain) {
                LoginMainScreen()
            }
        }

    }
}

//定义了一个NavHostController的扩展函数，扩展函数名为restoreStateOnReturn,带有一个String类型的route参数
// = 等号后面的this.navigate(route){}，相当于是这个新定义的扩展函数的具体的实现
//navigate是NavHostController的一个方法，调用navigate方法时，传入的第一个参数路由名，第二个参数是一个lambda，用来配置一些导航的行为
fun NavHostController.restoreStateOnReturn(route: String) = this.navigate(route){
    popUpTo(this@restoreStateOnReturn.graph.findStartDestination().id){
        saveState = true
    }
    launchSingleTop = true
    restoreState = true
}
